##################################################################
# File : /lua/maui/movie.lua
# Description : Movie control
# Author : GPG
##################################################################

local Control = import('control.lua').Control

Movie = Class(moho.movie_methods, Control) {
	--[[
	Methods:
		Set(string filename)
		Play()
		Stop()
		Loop(bool loop)
		number GetFrameRate()
		int GetNumFrames()
		IsLoaded()
		InternalSet()
	--]]

    __init = function(self, parent, filename)
        InternalCreateMovie(self, parent)
        if filename then
            self:Set(filename)
        end
    end,

    ResetLayout = function(self)
        Control.ResetLayout(self)
        self.Width:SetFunction(function() return self.MovieWidth() end)
        self.Height:SetFunction(function() return self.MovieHeight() end)
    end,

    OnInit = function(self)
        Control.OnInit(self)

    end,
    
    Set = function(self,filename)
		local ok = self:InternalSet(filename)
		if ok then
            ForkThread(
                function()
                    while true do
                        if self:IsLoaded() then
                            self:OnLoaded()
                            return
                        end
                        WaitSeconds(0.01)
                    end
                end
            )
        else
            # Force calls to OnStopped()
            self:OnStopped()
        end
	end,
    
    GetLength = function(self)
        return self:GetNumFrames() / self:GetFrameRate()
    end,

    # callback scripts
    OnFinished = function(self) end,
    OnStopped = function(self) end,

    # Called when a subtitle changes. string should be LOC()'d for display
    OnSubtitle = function(self,string) end,

    # Called when the movie is loaded and ready to play immediately
    OnLoaded = function(self) end,
}
